package net.showcal.open.api;

import com.showcal.platform.request.IMLoginRequest;
import com.showcal.platform.service.PlatformService;
import com.showcal.service.request.SendIMMessageRequest;
import com.showcal.service.service.ShowcalService;
import com.xiniunet.apiframework.AbstractApiRequest;
import com.xiniunet.apiframework.annotation.HttpAction;
import com.xiniunet.apiframework.annotation.ServiceMethod;
import com.xiniunet.apiframework.annotation.ServiceMethodBean;
import com.xiniunet.apiframework.response.ErrorResponse;
import com.xiniunet.framework.security.Passport;
import net.showcal.open.tool.RequestConverter;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

/**
 * 瘦咖消息服务
 * Created by guzhixiong on 2015/11/10.
 */
@ServiceMethodBean
public class IMServiceApi {
    @Autowired
    private ShowcalService showcalService;
    @Autowired
    private PlatformService platformService;
    @Autowired
    private RequestConverter requestConverter;

    /**
     * 登录到悟空
     */
    @ServiceMethod(title = "登录到悟空",method = "im.login",httpAction = HttpAction.POST,version = "1.0" )
    public Object IMLogin(AbstractApiRequest apirequest){
        Passport passport = requestConverter.requestToPassport(apirequest);
        IMLoginRequest iMLoginRequest = requestConverter.converter(IMLoginRequest.class,apirequest);
        HttpServletRequest request = (HttpServletRequest) apirequest.getApiRequestContext().getRawRequestObject();
        ErrorResponse response = requestConverter.doValidate(request, iMLoginRequest);
        if(response != null) {
            return response;
        } else {
           return platformService.IMLogin(iMLoginRequest, passport);
        }
    }

    /**
     * IM 发送问题
     */
    @ServiceMethod(title = "IM 发送问题",method = "im.questionForIM.send",httpAction = HttpAction.POST,version = "1.0" )
    public Object sendQuestionForIM(AbstractApiRequest apirequest){
        Passport passport = requestConverter.requestToPassport(apirequest);
        SendIMMessageRequest sendIMMessageRequest = requestConverter.converter(SendIMMessageRequest.class,apirequest);
        HttpServletRequest request = (HttpServletRequest) apirequest.getApiRequestContext().getRawRequestObject();
        ErrorResponse response = requestConverter.doValidate(request, sendIMMessageRequest);
        if(response != null) {
            return response;
        } else {
            return showcalService.sendQuestionForIM(sendIMMessageRequest, passport);
        }
    }

}
